package inftel.android;


import inftel.android.R.id;

import java.io.DataInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.Toast;

public class Bienvenido extends TabActivity{

	
	private ListView listaComentarios;
	private List<Comentario> comentarios;
	private String email; 
	private final String ip = "192.168.2.105";
	private final String puerto = "14067";
	private String servletURL = "http://"+ip+":"+puerto+
	"/ServiciosRedSocial-war/MuroServlet";
	private ComentariosAdapter adapter1;
	
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.bienvenido);
	    Bundle parametros = this.getIntent().getExtras();
	    email = parametros.getString("email");
	    listaComentarios = (ListView) findViewById(id.comentariosMuro);
	    TabHost mTabHost = getTabHost();
	    
	    ArrayList<HashMap<String, Object>> contentsComentarios = new ArrayList<HashMap<String, Object>>();
	    obtenerComentarios();
	    Iterator<Comentario> it = comentarios.iterator();
	    while(it.hasNext()){
	    	Comentario c = it.next();
        	HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("FOTO", c.getFotoPerfil());
			map.put("CREADOR", c.getAutor());
			map.put("CONTENT", c.getMensaje());
			contentsComentarios.add(map);
        }
	    
        adapter1 = new ComentariosAdapter(new LinkedList<Comentario>(),this,
        		contentsComentarios, R.layout.fila_comentarios_layout,
				new String[] { "FOTO", "CREADOR","CONTENT"}, new int[] {
						R.id.fila_comentarios_listitem_pic, R.id.fila_comentarios_listitem_autor, R.id.fila_comentarios_listitem_mensaje});
	    
	    listaComentarios.setAdapter(adapter1);
	    
	    mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("Muro").setContent(R.id.comentariosMuro));
	    mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("Información").setContent(R.id.textView2));
	    mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("Fotos").setContent(R.id.textView3));
	    
	    mTabHost.setCurrentTab(0);	    
	    
	    
	    try {
		  			
	} catch (Exception e) {
		Log.i("INFO", "Exception:" + e);
		e.printStackTrace();
	}
	    
	}
	
public void obtenerComentarios(){
		
		Comentario comment;
		
		comentarios = new LinkedList<Comentario>();
				
		try{
			
			  Log.i("Conectando","Conectando al MuroServlet");
		    	ProgressDialog dialog = ProgressDialog.show(Bienvenido.this, "","Cargando comentarios...", false,true);
				URL ob = new URL(servletURL + "?email=" + email);
				URLConnection conn = ob.openConnection();
				conn.setDoInput(true);
				conn.setDoOutput(true);
				InputStream in = conn.getInputStream();
				DataInputStream dis = new DataInputStream(in);
				int tam = Integer.parseInt(dis.readLine());		
							
				//dialog.hide();
				dialog.dismiss();
			if (tam == 0) {
				CharSequence text = "No hay comentarios...";
				int duration = Toast.LENGTH_LONG;
				Toast toast = Toast.makeText(Bienvenido.this, text,duration);
				toast.show();
			} else {
				listaComentarios = (ListView) findViewById(R.id.comentariosMuro);
				for(int i=0;i<tam;i++){
					comment = new Comentario();
					String comentarioLeido = dis.readLine();
					JSONObject j = new JSONObject(comentarioLeido);
					JSONObject creador = (JSONObject) j.get("usuario1");
					comment.setNombreAutor(creador.getString("nombre"));
					comment.setApellidosAutor(creador.getString("apellidos"));
					String foto = (String)(creador.get("fotoPerfil"));
					comment.setFotoPerfil(foto.replace("localhost:8080", ip+":"+puerto));
					comment.setMensaje(j.getString("mensaje"));
					comentarios.add(comment);
				}
				    
				CharSequence text = tam + " Comentarios cargados";
				int duration = Toast.LENGTH_LONG;
				Toast toast = Toast.makeText(Bienvenido.this, text,duration);
				toast.show();			
			}
			dis.close();
			conn = null;
	        
		}catch(Exception e){ 
            e.printStackTrace(); 
		} 
	}
}

